﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Classes.Model.Dao;
using Classes.Model.Entidade;
using Juntae.Util;
using System.IO;

namespace Juntae
{
    public partial class CadastroEvento : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            this.carregaDropDownlist();
        }

        protected void salvarEvento(object sender, EventArgs e)
        {
            EventoDAO.getInstance().inserir(carregaInfoEvento());
        }

        protected void carregaDropDownlist()
        {
            CategoriaDAO cat = new CategoriaDAO();
            dropCategoria.DataSource = cat.select();
            dropCategoria.DataBind();

            ClassificacaoDAO cla = new ClassificacaoDAO();
            dropClassificacao.DataSource = cla.select();
            dropClassificacao.DataBind();

            GeneroDAO gen = new GeneroDAO();
            dropGenero.DataSource = gen.select();
            dropGenero.DataBind();

            dropStatus.DataSource = StatusDAO.getInstance().select();
            dropStatus.DataBind();

            dropCidade.DataSource = CidadeDAO.getInstance().select();
            dropCidade.DataBind();
        }

        protected EventoDTO carregaInfoEvento()
        {
            EventoDTO eve = new EventoDTO();
            eve.Nome = txtNomeEvento.Text;
            eve.Descricao = txtDescricaoEvento.Text;
            eve.Site = txtSite.Text;
            eve.Responsavel = txtResponsavel.Text;
            eve.Email = txtEmail.Text;
            eve.Telefone = txtContato.Text;
            eve.Logomarca = "TESTE";
            eve.Categoria = CategoriaDAO.getInstance().select(Convert.ToInt16(dropCategoria.SelectedValue));
            eve.Genero = GeneroDAO.getInstance().select(Convert.ToInt16(dropGenero.SelectedValue));
            eve.Classificacao = ClassificacaoDAO.getInstance().select(Convert.ToInt16(dropClassificacao.SelectedValue));
            eve.Status = StatusDAO.getInstance().select(Convert.ToInt16(dropStatus.SelectedValue));
            eve.Cidade = CidadeDAO.getInstance().select(Convert.ToInt16(dropCidade.SelectedValue));
            eve.Data_final = Convert.ToDateTime(txtDataFim.Text);
            eve.Horario = Convert.ToDateTime(txtHorario.Text);
            eve.Programacao = TextAreaProgramacao.InnerText;
            eve.Local = TextAreaLocal.InnerText;
            eve.Valor = TextAreaValor.InnerText;

            return eve;
        }

        protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            try
            {
                /*string filename = Membership.GetUser(true).ProviderUserKey.ToString();
                string extension = e.filename.Substring(e.filename.LastIndexOf("."));
                if (extension.Equals(".jpg") || extension.Equals(".JPG"))
                {
                    string strPath = MapPath("~/img/Eventos/") + Path.GetFileName(filename + extension);
                    AsyncFileUpload1.SaveAs(strPath);
                    ResizeImage.Resize(strPath, 117, 117);
                    
                }*/
            }
            catch (Exception ex)
            {
                if (!ClientScript.IsClientScriptBlockRegistered("falhauploadfoto"))
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "falhauploadfoto", ScriptUtil.MsgCustom("Erro ao enviar foto. " + ex.Message));
                }
                return;
            }
        }
    }
}